<template>
  <div>
    <!--  -->
    <div class="ding">
      loading:{{ loading }}===finished:{{ finished }}===refreshing:
    </div>
    <!--  -->
    <div class="shuju">
      <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          offset="100"
          @load="onLoad"
        >
          <van-cell
            v-for="(item, index) in list"
            :key="item.good_id"
            :title="index + 1 + '.===>' + item.goods_name"
          />
        </van-list>
      </van-pull-refresh>
    </div>
  </div>
</template>
<script>
import axios from 'axios';
export default {
  data () {
    return {
      list: [],
      loading: false,//加载数据，不加载了为true
      finished: false,//数据加载完毕为true,否则为false
      tou: 0,
      wei: 10,
      refreshing: false,//表示是否正在下拉刷新,刷新时为true,刷新后为false
    }
  },
  methods: {
    onLoad () {
      // this.loading = false//上提页面数据正在加载时该值为true
      // 数据全部加载完成
      if (this.tou >= 5) {
        this.finished = true//当finished为fals的时候页面数据底部无法再加载
        this.loading = false
        // this.refreshing = false
        return
      }
      this.tou += 1
      this.fn()

    },
    async fn () {
      // 请求数据
      const res = await axios({
        url: 'http://vue-shop-api-t.itheima.net/api/private/v1/goods',
        headers: {
          Authorization: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjUwMCwicmlkIjowLCJpYXQiOjE2ODU0MzEyOTgsImV4cCI6MTY4NTUxNzY5OH0.rMLF5nxywTFwqBVP7RWyNJ_qEXoleuetLMbAHDz-NvI'
        },
        params: {
          pagenum: this.tou,
          pagesize: this.wei
        }
      })
      // console.log(res.data.data.goods)
      this.list = this.refreshing ? res.data.data.goods : [...this.list, ...res.data.data.goods]
      this.list = [...this.list, ...res.data.data.goods]
      this.loading = false//结束当前加载
      this.refreshing = false//关闭下拉
    },
    onRefresh () {
      this.tou = 0// 重新加载数据
      this.onLoad()//再次调用
    },
  }
}
</script>
<style lang='less'  scoped>
.ding {
  position: fixed;
  top: 0;
  left: 0;
  background-color: pink;
  z-index: 999;
  width: 100vw;
}
.shuju {
  margin-top: 50px;
}
</style>
